﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Omu.ValueInjecter;

namespace Icms.Data
{
    public class SetParamsValues : KnownTargetValueInjection<SqlCommand>
    {
        private string prefix = string.Empty;

        public SetParamsValues Prefix(string p)
        {
            prefix = p;
            return this;
        }
        
        protected override void Inject(object source, ref SqlCommand cmd)
        {
            if (source == null) return;
            var d = source as Dictionary<string, object>;

            foreach (var o in d)
            {
                var value = o.Value ?? DBNull.Value;
                cmd.Parameters.AddWithValue("@" + prefix + o.Key, value);
                Console.WriteLine("add par: @"+prefix+o.Key + " = "+value);
            }
        }
    }
}